package com.szsfm.vehiclemanagement.manager;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.orhanobut.logger.Logger;
import com.szsfm.vehiclemanagement.listener.LocationListener;

/**
 * Created by zhoubigbo on 2017/11/14.
 */
public class BDLocationManager {

    private static BDLocationManager instance;

    public LocationClient mLocationClient = null;
    private LocationListener listener;

    private BDLocationManager() {
    }

    public static BDLocationManager getInstance() {
        synchronized (BDLocationManager.class) {
            if (instance == null) {
                synchronized (BDLocationManager.class) {
                    instance = new BDLocationManager();
                }
            }
        }
        return instance;
    }

    public void initSDK(Context context){
        SDKInitializer.initialize(context);
    }

    public void init(Context context) {
        if (mLocationClient == null) mLocationClient = new LocationClient(context);
        mLocationClient.registerLocationListener(bdLocationListener);
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd09ll");
        option.setScanSpan(1000);
        option.setOpenGps(true);
        option.setLocationNotify(true);
        option.setIgnoreKillProcess(false);
        option.setEnableSimulateGps(false);
        option.setIsNeedLocationDescribe(true);
        mLocationClient.setLocOption(option);
    }

    public void setLocationListener(LocationListener l){
        this.listener = l;
    }

    BDLocationListener bdLocationListener = new BDLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
           if (listener!=null){
               double lat = bdLocation.getLatitude();
               double lnt = bdLocation.getLongitude();
               String place = bdLocation.getLocationDescribe();
               listener.onReceiveLocation(lat,lnt,place);
           }
        }
    };

    public void start() {
        if (mLocationClient != null && !mLocationClient.isStarted()) mLocationClient.start();
    }

    public void stop() {
        if (mLocationClient != null && mLocationClient.isStarted()) {
            mLocationClient.stop();
        }
    }

    public void latlng2Address(double lat, double lng,final ReverseAddressListener l){
        ReverseGeoCodeOption reverseGeoCodeOption = new ReverseGeoCodeOption();
        reverseGeoCodeOption.location(new LatLng(lat, lng));
        GeoCoder coder=GeoCoder.newInstance();
        coder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
            @Override
            public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
                Logger.d("GeoCodeResult="+geoCodeResult.toString());
            }

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
                Logger.d("reverseGeoCodeResult="+reverseGeoCodeResult.getSematicDescription());
                if (l!=null){
                    l.callback(reverseGeoCodeResult.getSematicDescription());
                }
            }
        });
        coder.reverseGeoCode(reverseGeoCodeOption);
    }

    public interface ReverseAddressListener{
        void callback(String address);
    }

}
